package com.td.server.obj;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.krakers.evo.obj.DNA;
import com.td.evo.obj.factory.TDFactory;

public class TDBotFactory {

	private static TDBotFactory factory = new TDBotFactory();
	
	private static final String FILE = "file/DNA.txt";
	
	private Random r;
	
	private TDFactory dnaFactory;
	
	private List<DNA> dna;
	
	private TDBotFactory() {
		dnaFactory = new TDFactory(null);
		dna = new ArrayList<DNA>();
		r = new Random(new Date().getTime());
		
		loadFromFile();
	}
	
	public static TDBotFactory getInstance() {
		return factory;
	}
	
	public TDBot createTDBot() {
		int i = r.nextInt(dna.size());
		return new TDBot(dna.get(i), TDBot.NORMAL);
	}
	
	public void loadFromFile() {
		dna.clear();
		
		BufferedReader file = null;
		try {
			file = new BufferedReader(new FileReader(FILE));
			
			String line = null;
			while((line = file.readLine()) != null) {
				dna.add(dnaFactory.createSpecimen(line));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				file.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
